package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.UserDelegate;
import coms.dto.Email;
import coms.dto.User;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.JMSException;

public class UserAction extends ComsActionSupport implements IAction {

    private int userId;
    private String message;
    private User user;
    private List<User> userList;
    private UserDelegate userDelegate;
    private Email email;

    private String action;
    private String subject;
    private boolean admin;

    public boolean isAdmin() {
         User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } 
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }
    
    public UserAction() {
        user = new User();
        userDelegate = new UserDelegate();
        userList = new ArrayList<User>();
        email = new Email();
    }

    private void initForm() {
       
    }
    
     private void populateDefault() {
        user = new User();
  
    }
    
    private void populate(){
        try {
            if(userId==-1)
            {
                User usr=(User)getSession().get("user");
                userId=usr.getId();
            }
            else
                user = userDelegate.getById(userId);
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {

        initForm();
        populateDefault();
        action = "createUser_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updateUser_" + user.getId();
        return "edit";
    }
    
    public String send(){
        populate();
        action = "sendUser_" + user.getId();
        return "send";
    }

    @Override
    public String create() {

        userDelegate.create(user);
        getSession().put("user",user);
        return Action.SUCCESS;
    }

    @Override
    public String update() {
        try {
            System.out.println("############## update id:" + user.getId());
             userDelegate.update(user);
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return "updated";
    }

    @Override
    public String delete() {
         populate();
        userDelegate.delete(user);
        return "deleted";
    }

    @Override
    public String findAll() {
        try {
            userList = userDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {
        try {
            userList = userDelegate.search(user);
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
        }
  
        return "search";
    }
    
    public String emailSend() throws JMSException{
        try {
            user = userDelegate.getByUsername(user.getUsername());
        } catch (Exception ex) {
            Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        email.setMessage(getMessage());
        email.setSubject(getSubject());
        email.setTo(user.getEmailAddress());
        userDelegate.sendEmail(email);
        return Action.SUCCESS;
    }

    public User getUser() {
        return user;
    }

    public Email getEmail(){
        return email;
    }
    public void setUser(User user) {
        this.user = user;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public void setUserId(String userId) {
       try{
       this.userId = Integer.parseInt(userId);
       user.setId(this.userId);
       } catch (Exception ex){
           Logger.getLogger(UserAction.class.getName()).log(Level.SEVERE, null, ex);
       }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getUserId() {
        return "" + userId;
    }
    
    public void setMessage(String message){
        this.message = message; 
    }
    
    public void setSubject(String subject){
        this.subject = subject;
    }
    
    public String getMessage(){
        return "" + message;
    }
    
    private String getSubject(){
        return "" + subject;
    }
}
